"""
Task 2.2: Send Data Using POST Requests

Goal: Learn to send data to an API using POST requests.

Exercises:
- Create a new post
- Add a new comment to a post
- Create a new user
"""

import httpx

# Exercise 2.1: Create a new post
url = "https://jsonplaceholder.typicode.com/posts"
# TODO: Define new post data and send POST request

data = {
    "title":"my favorite girls group",
    "body":"blackpink",
    "userId": 1
}

response = httpx.post(url, json=data)
created_post = response.json()

print(f" ID: {created_post['id']}")
print(f" title: {created_post['title']}")
print(f" body: {created_post['body']}")
print(f" userId: {created_post['userId']}")


# Exercise 2.2: Add a new comment to a post
url = "https://jsonplaceholder.typicode.com/comments"
# TODO: Define new comment data and send POST request

new_comment = {
    "postId":1,
    "name":"zy",
    "email": "zy@gardner.biz",
    "body":"I really want to have the first row of the concert "
}

response = httpx.post(url, json = new_comment)
created_comment = response.json()

print(f"   ID: {created_comment['id']}")
print(f"   postID: {created_comment['postId']}")
print(f"   name: {created_comment['name']}")
print(f"   email: {created_comment['email']}")
print(f"   body: {created_comment['body']}")

# Exercise 2.3: Create a new user
url = "https://jsonplaceholder.typicode.com/users"
# TODO: Define new user data and send POST request

user_data = {
    "id": 1,
    "name": "zy",
    "username": "zyy",
    "email": "zy@april.biz",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874",
      "geo": {
        "lat": "-37.3159",
        "lng": "81.1496"
      }
    },
    "phone": "1234567890",
    "website": "hhh.org",
    "company": {
      "name": "Romaguera-Crona",
      "catchPhrase": "Multi-layered client-server neural-net",
      "bs": "harness real-time e-markets"
    }
  }

response = httpx.post(url, json = user_data)
created_user = response.json()

print(f"   用户ID: {created_user['id']}")
print(f"   姓名: {created_user['name']}")
print(f"   用户名: {created_user['username']}")
print(f"   邮箱: {created_user['email']}")
print(f"   电话: {created_user['phone']}")
print(f"   城市: {created_user['address']['city']}")  
print(f"   公司: {created_user['company']['name']}")  